﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JT.Data;
using JT.Domain;
using JT.Domain.TimeLine;
using JTWEB3.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace JT.WEB.Models.Data
{
    public static class SeedData
    {

        public static void Initialize(IServiceProvider serviceProvider)
        {
            using ContextBase context = new ContextBase(serviceProvider.GetRequiredService<DbContextOptions<ContextBase>>());
            context.Database.Migrate();

            // Look for any movies.
            //if (!context.Years.Any())
            //{
            //    var y = new Year { YearId = 2013 };
            //    context.Years.Add(y);
            //    context.SaveChanges();
            //}
            //if (!context.TimeLines.Any())
            //{

            //    var t = new TimeLine
            //    {
            //        Months = Month.Aug,
            //        Content = "精泰检测成立了！",
            //        YearId = 2013,
            //        YearNavigation = context.Years.FirstOrDefault(m => m.YearId == 2013)
            //    };
            //    context.TimeLines.Add(t);
            //    context.SaveChanges();
            //}

            //if (!context.Messages.Any())
            //{
            //    var t = new Message { Content = "欢迎来到精泰检测！", Title = "欢迎来到精泰检测", MSLevel = EMSLevel.SystemMassage };
            //    context.Messages.Add(t);
            //    context.SaveChanges();
            //}

            //using var usercontext = new UserContext(serviceProvider.GetRequiredService<DbContextOptions<UserContext>>());
            //if (!usercontext.Roles.Any())
            //{
            //    var role = new IdentityRole { Name = "Administrators", NormalizedName = "ADMINISTRATORS" };
            //    usercontext.Roles.AddRange(
            //        new IdentityRole { Name = "Administrators", NormalizedName = "ADMINISTRATORS" },
            //        new IdentityRole { Name = "WereHouse", NormalizedName = "WEREHOUSE" }
            //    );
            //    usercontext.SaveChanges();
            //}

            //if (!usercontext.JTWEB3Users.Any())
            //{
            //    var user = new JTWEB3User { UserName = "admin@qq.com", Email = "admin@qq.com", Password = "123456", NickName = "管理员" };
            //    usercontext.JTWEB3Users.Add(user);
            //    usercontext.SaveChanges();
            //}

            //if (!usercontext.UserRoles.Any())
            //{
            //    var user = usercontext.Users.FirstOrDefault(t => t.Email == "admin@qq.com");
            //    var role = usercontext.Roles.FirstOrDefault(t => t.Name == "Administrators");
            //    var userrole = new IdentityUserRole<string> { UserId = user.Id, RoleId = role.Id };
            //    usercontext.UserRoles.Add(userrole);
            //    usercontext.SaveChanges();
            //}
        }
    }

}
